package com.tuanzi.loan.web.bean.family.largeamount;

import com.tuanzi.loan.business.entity.family.LoanLargeOuterTemplate;
import com.tuanzi.loan.business.service.family.LoanLargeOuterTemplateService;
import com.tuanzi.loan.vo.request.family.LoanLargeOuterTempLateListRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeOuterTempLateListResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.largeAmount.LoanLargeOuterTemplateListLazyDataModel;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * User: liangZongXi
 * Create time: 15:58 2019/6/4
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)

public class LoanLargeOuterTemplateListBean extends JSFListingBean<LoanLargeOuterTempLateListRequest, LoanLargeOuterTempLateListResponse> {
    private static final long serialVersionUID = 5911028244355387843L;

    @Autowired
    private LoanLargeOuterTemplateService loanLargeOuterTemplateService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    private void init(){
        $ = new LoanLargeOuterTempLateListRequest();
        lazyDataModel = new LoanLargeOuterTemplateListLazyDataModel($);
    }

    public void onRest(){
        init();
    }

    public String add(){
        return redirect("/views/family/extratemplate/large-outer-template-info.xhtml");
    }

    public String toInfo(Long id){
        return redirect("/views/family/extratemplate/large-outer-template-info.xhtml?id=" + id);
    }

    public void onDelete(Long id){
        if(null == id){
            init();
            return;
        }
        LoanLargeOuterTemplate one = loanLargeOuterTemplateService.findOne(id);
        if(null == one){
            init();
            return;
        }

        stringRedisTemplate.delete(LoanRedisKey.LOAN_LARGE_OUTER_TEMPLATE + one.getTemplateId());
        loanLargeOuterTemplateService.delete(one);
        init();
    }

    public void onCopy(Long id){
        LoanLargeOuterTemplate one = loanLargeOuterTemplateService.findOne(id);
        LoanLargeOuterTemplate loanLargeOuterTemplate = new LoanLargeOuterTemplate();
        BeanUtils.copyProperties(one,loanLargeOuterTemplate,"id","templateId","creator",
                "createTime","updator","updateTime","version");
        loanLargeOuterTemplateService.saveOrUpdate(loanLargeOuterTemplate);
        addSuccessfullyMessage();
    }

}
